<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column label="序号" width="120">
                <template scope="scope">
                  <span>{{ (curPage - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hEdit(scope.row)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(scope.row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="curPage"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="isShowDialog"
        @close="hClose"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, delRole, addRole, updateRole } from '@/api/settings'

export default {
  data() {
    return {
      total: 0, // 总数量
      roles: [], // 保存角色列表
      curPage: 1, // 当前是第几页
      pageSize: 2, // 当前几条
      isShowDialog: false, // 显示隐藏弹框
      isEdit: false, // 判断是否为编辑
      // 表单数据
      roleForm: {
        name: '',
        description: ''
      },
      // 表单校验规则
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      const { data } = await getRoles(this.curPage, this.pageSize)
      console.log(data)
      // 绑定数据
      this.roles = data.rows
      this.total = data.total
    },
    handleSizeChange(val) {
      console.log(`每页${val}条`)
      this.curPage = 1
      this.pageSize = val
      this.loadRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.curPage = val
      this.loadRoles()
    },
    async doDel(id) {
      await delRole(id)
    },
    hDel(id) {
      // 弹层询问，是否退出
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.doDel(id)
          this.$message.success('删除成功')
          this.loadRoles()
        })
        .catch(() => {
          // 用户取消退出
        })
    },
    async doAdd() {
      await addRole(this.roleForm)
      this.$message.success('添加成功')
      this.isShowDialog = false
      // 添加后页面跳到最后一页
      this.total++
      this.curPage = Math.ceil(this.total / this.pageSize)
      // 重新渲染
      this.loadRoles()
    },
    async doEdit() {
      await updateRole(this.roleForm)
      this.loadRoles()
      this.$message.success('编辑成功')
      this.isShowDialog = false
    },
    // 点击新增
    hAdd() {
      this.isEdit = false
      this.isShowDialog = true
    },
    // 点击编辑
    hEdit(row) {
      this.isEdit = true
      // 回显数据
      this.roleForm = { ...row }
      // 显示弹框
      this.isShowDialog = true
    },
    // 点击确认新增角色
    hSubmit() {
      // 兜底校验成功后添加角色
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 弹框关闭后清空表单内容
    hClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    }
  }
}
</script>
